package com.wxzz.elearing.user.api.admin;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.user.dto.SystemMessageDto;
import com.wxzz.elearing.user.dto.SystemMessageSelDto;
import com.wxzz.elearing.user.entity.SystemMessage;
import com.wxzz.elearing.user.service.ISystemMessageService;
import com.wxzz.elearning.common.aop.Idempotent;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 通知 前端控制器
 * </p>
 *
 * @author
 * @since 2021-05-22
 */
@RestController
@RequestMapping("/system-message")
@Api(tags = "消息通知(后台)")
public class SystemMessageController {

    @Autowired
    private ISystemMessageService systemMessageService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Idempotent
    @ApiOperation(value = "添加或修改消息通知", notes = "添加或修改消息通知", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveSystemMessage", method = RequestMethod.POST)
    public ReturnVO saveSystemMessage(@RequestHeader String token, @RequestBody SystemMessage systemMessage) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        systemMessage.setType(0);
        return systemMessageService.saveSystemMessage(jsonObject.getLong("enterpriseId"), systemMessage);
    }

    @ApiOperation(value = "根据id查询消息详情", notes = "根据id查询消息详情", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectSystemMessageById/{systemMessageId}", method = RequestMethod.POST)
    public ReturnVO<SystemMessage> selectSystemMessageById(@RequestHeader String token, @PathVariable Long systemMessageId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return systemMessageService.selectSystemMessageById(jsonObject.getLong("enterpriseId"), systemMessageId);
    }

    @ApiOperation(value = "根据当前后台身份列出通知列表", notes = "根据当前后台身份列出通知列表", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listSystemMessage", method = RequestMethod.POST)
    public ReturnVO listSystemMessage(@RequestHeader String token, @RequestBody SystemMessageSelDto systemMessageSelDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return systemMessageService.listSystemMessage(jsonObject.getLong("enterpriseId"), systemMessageSelDto);
    }

    @ApiOperation(value = "查询发送通知绑定的用户", notes = "查询发送通知绑定的用户", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectSystemMessage/{systemMessageId}", method = RequestMethod.POST)
    public ReturnVO selectSystemMessage(@PathVariable Long systemMessageId) {
        return systemMessageService.selectSystemMessageByWxUserId(systemMessageId);
    }

    @ApiOperation(value = "查询发送通知绑定的企业", notes = "查询发送通知绑定的企业", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectSystemMessageByEnterpriseId/{systemMessageId}", method = RequestMethod.POST)
    public ReturnVO selectSystemMessageByEnterpriseId(@PathVariable Long systemMessageId) {
        return systemMessageService.selectSystemMessageByEnterpriseId(systemMessageId);
    }

    @ApiOperation(value = "收到的通知列表(企业)", notes = "收到的通知列表(企业)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "acceptListSystemMessage", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SystemMessage>>> acceptListSystemMessage(@RequestHeader String token, @RequestBody SystemMessageDto systemMessageDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return systemMessageService.listSystemMessage(jsonObject.getLong("enterpriseId"), systemMessageDto);
    }

    @ApiOperation(value = "收到的通知列表(平台)", notes = "收到的通知列表(平台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listSystemMessage4platform", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SystemMessage>>> listSystemMessage4platform(@RequestHeader String token, @RequestBody SystemMessageDto systemMessageDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return systemMessageService.listSystemMessage4platform(0L, systemMessageDto);
    }

    @ApiOperation(value = "一键已读(后台)", notes = "一键已读(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "allRead", method = RequestMethod.POST)
    public ReturnVO allRead(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return systemMessageService.allRead(jsonObject.getLong("enterpriseId"));
    }

}
